//
//  GGmoneyCollectionViewCell.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/2.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "GGmoneyCollectionViewCell.h"
#import "GGMoneyModel.h"
#import "GGmoneyTableViewCell.h"

@interface GGmoneyCollectionViewCell ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property(nonatomic, strong)ExceptionView * exceptionView;

@end

@implementation GGmoneyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self initView];
    }
    return self;
}


- (void)initView{
    
    
    [self setProperty];
    
    [self createUI];
    
    [self refreashData];
    
}


#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.backgroundColor=RGB(255, 255, 255);
    
}

#pragma mark==================createUI======================
-(void)createUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreashData];
    }];
    //    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];
    
}

#pragma mark-下拉刷新
- (void)refreashData{
    
    NSString * URL = [NSString stringWithFormat:@"%@leverassets/get",DEFAULTSEVERBASEURL];
    WS(weakSelf);
    [HUD showProgress :@"" inView:self];
    [MOLoadHttpsData GetHttpDataWithUrlStr:URL Dic:@{} SuccessBlock:^(id  _Nonnull responseObject) {
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSDictionary * dicc = dic[@"data"];
            GGMoneyModel * GGModel = [GGMoneyModel sharedGGMoneyModel];
            GGModel = [GGMoneyModel mj_objectWithKeyValues:dicc];
            
            [weakSelf relayOutUI];
        }
        
        [HUD dismissInView:weakSelf];
        [weakSelf.tableView.mj_header endRefreshing];
        
    } FailureBlock:^(id  _Nonnull error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [HUD dismissInView:weakSelf];
        
    }];
}
-(void)loadMoreData{
    
}

-(void)relayOutUI{
    WS(wself);
    GGMoneyModel * GGModel = [GGMoneyModel sharedGGMoneyModel];
        if (GGModel.list.count<1) {
    
            [self.tableView reloadData];
    
            self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
            self.exceptionView.heightH = G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight-kTabBarHeight-120;
    
            [self.exceptionView showInView:wself.tableView];
        }else{
            [self.exceptionView dismiss];
            self.exceptionView = nil;
            [self.tableView reloadData];
        }
    
}

#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    GGMoneyModel * GGModel = [GGMoneyModel sharedGGMoneyModel];
    return GGModel.list.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GGmoneyTableViewCell computeHeight:@""];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *reuseIdentifier =[NSString stringWithFormat:@"GGmoneyTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    
    GGmoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GGmoneyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    GGMoneyModel * Bmodel = [GGMoneyModel sharedGGMoneyModel];
    WS(weakSelf);
    if (Bmodel.list.count>0) {
        cell.dataSource=Bmodel.list[indexPath.row];
    }
    cell.block = ^(GGlistMoneyModel * _Nonnull model, NSString * _Nonnull Type) {
        if (weakSelf.block) {
            weakSelf.block(model,Type);
        }
    };
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}




/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


